package com.ruoyi.project.data.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.data.domain.TbTransportDriverRequirements;
import com.ruoyi.project.data.service.ITbTransportDriverRequirementsService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 人员从业资格Controller
 *
 * @author ruoyi
 * @date 2020-03-23
 */
@RestController
@RequestMapping("/system/requirements")
public class TbTransportDriverRequirementsController extends BaseController
{
    @Autowired
    private ITbTransportDriverRequirementsService tbTransportDriverRequirementsService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询人员从业资格列表
     */
    @PreAuthorize("@ss.hasPermi('weight:requirements:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbTransportDriverRequirements tbTransportDriverRequirements)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        tbTransportDriverRequirements.setRole(user.getRoles().get(0).getRoleKey());
        startPage();
        List<TbTransportDriverRequirements> list = tbTransportDriverRequirementsService.selectTbTransportDriverRequirementsList(tbTransportDriverRequirements);
        return getDataTable(list);
    }

    /**
     * 导出人员从业资格列表
     */
    @PreAuthorize("@ss.hasPermi('weight:requirements:export')")
    @Log(title = "人员从业资格", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbTransportDriverRequirements tbTransportDriverRequirements)
    {
        List<TbTransportDriverRequirements> list = tbTransportDriverRequirementsService.selectTbTransportDriverRequirementsList(tbTransportDriverRequirements);
        ExcelUtil<TbTransportDriverRequirements> util = new ExcelUtil<TbTransportDriverRequirements>(TbTransportDriverRequirements.class);
        return util.exportExcel(list, "requirements");
    }

    /**
     * 获取人员从业资格详细信息
     */
    @GetMapping(value = "/{requirementId}")
    public AjaxResult getInfo(@PathVariable("requirementId") Long requirementId)
    {
        return AjaxResult.success(tbTransportDriverRequirementsService.selectTbTransportDriverRequirementsById(requirementId));
    }

    /**
     * 新增人员从业资格
     */
    @PreAuthorize("@ss.hasPermi('weight:requirements:add')")
    @Log(title = "人员从业资格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbTransportDriverRequirements tbTransportDriverRequirements)
    {
        return toAjax(tbTransportDriverRequirementsService.insertTbTransportDriverRequirements(tbTransportDriverRequirements));
    }

    /**
     * 修改人员从业资格
     */
    @PreAuthorize("@ss.hasPermi('weight:requirements:edit')")
    @Log(title = "人员从业资格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbTransportDriverRequirements tbTransportDriverRequirements)
    {
        return toAjax(tbTransportDriverRequirementsService.updateTbTransportDriverRequirements(tbTransportDriverRequirements));
    }

    /**
     * 删除人员从业资格
     */
    @PreAuthorize("@ss.hasPermi('weight:requirements:remove')")
    @Log(title = "人员从业资格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{requirementIds}")
    public AjaxResult remove(@PathVariable Long[] requirementIds)
    {
        return toAjax(tbTransportDriverRequirementsService.deleteTbTransportDriverRequirementsByIds(requirementIds));
    }

    /**
     * 查询驾驶员列表
     */
    @GetMapping("/selectDriverList")
    public TableDataInfo selectDriverList(TbTransportDriverRequirements tbTransportDriverRequirements)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色判断
        if(permissionService.hasRole("admin")){    //管理员
        }else if(permissionService.hasRole("station") || permissionService.hasRole("hs_station") || permissionService.hasRole("xq_station")
                || permissionService.hasRole("jy_station") || permissionService.hasRole("yx_station")){     //场站
            Long stationId = user.getStationId();
            tbTransportDriverRequirements.setStationId(stationId);
        }else if(permissionService.hasRole("wxygc") || permissionService.hasRole("emergency_manage") || permissionService.hasRole("traffic_manage")
                || permissionService.hasRole("xqygc") || permissionService.hasRole("jyygc") || permissionService.hasRole("yxygc")){     //运管
        }
        startPage();
        List<TbTransportDriverRequirements> list = tbTransportDriverRequirementsService.selectDriverList(tbTransportDriverRequirements);
        return getDataTable(list);
    }
}
